{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:36.916993Z",
     "start_time": "2025-07-03T03:16:36.911345Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import matplotlib as plt\n",
    "import pandas as pd\n",
    "import os\n",
    "from math import sqrt\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
    "from Model import IMVTensorLSTM\n",
    "import torch\n",
    "print(torch.cuda.device_count())\n",
    "print(torch.cuda.is_available())\n",
    "from torch import nn\n",
    "from torch.utils.data import TensorDataset, DataLoader"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "False\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:17:22.831630Z",
     "start_time": "2025-07-03T03:17:22.813827Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.read_csv(r\"E:\\code\\Battery\\data\\battery_features_norm_10_1525.csv\")\n",
    "## 第一组\n",
    "# train_bound = 857\n",
    "# val_bound = 1730\n",
    "# test_bound_1 = 2594\n",
    "# test_bound_2 = 3368\n",
    "# test_bound_3 = 4450\n",
    "# Index = [0,2,6,8,10,11]\n",
    "## 第二组\n",
    "# train_bound = 761\n",
    "# val_bound = 1504\n",
    "# test_bound_1 = 2411\n",
    "# test_bound_2 = 3159\n",
    "# test_bound_3 = 3775\n",
    "# Index = [0,1,2,6,8,10]\n",
    "## 第三组\n",
    "# train_bound = 778\n",
    "# val_bound = 1485\n",
    "# test_bound_1 = 2093\n",
    "# test_bound_2 = 2769\n",
    "# test_bound_3 = 3508\n",
    "# Index = [2,6,0,10,8,11]\n",
    "## 第四组\n",
    "# train_bound = 657\n",
    "# val_bound = 1187\n",
    "# test_bound_1 = 1859\n",
    "# test_bound_2 = 2360\n",
    "# test_bound_3 = 2920\n",
    "# Index = [2,0,6,10,8,5]\n",
    "## 第五组\n",
    "# train_bound = 914\n",
    "# val_bound = 1592\n",
    "# test_bound_1 = 2581\n",
    "# test_bound_2 = 3670\n",
    "# test_bound_3 = 4421\n",
    "# Index = [2,0,6,7,11,10]\n",
    "## 第六组\n",
    "# train_bound = 443\n",
    "# val_bound = 897\n",
    "# test_bound_1 = 1374\n",
    "# test_bound_2 = 1880\n",
    "# test_bound_3 = 2480\n",
    "# Index = [0,2,4,5,6,11]\n",
    "## 第七组\n",
    "# train_bound = 819\n",
    "# val_bound = 1591\n",
    "# test_bound_1 = 2630\n",
    "# test_bound_2 = 3459\n",
    "# test_bound_3 = 4402\n",
    "# Index = [2,9,0,6,8,11]\n",
    "## 第八组\n",
    "# train_bound = 910\n",
    "# val_bound = 1738\n",
    "# test_bound_1 = 2730\n",
    "# test_bound_2 = 3485\n",
    "# test_bound_3 = 4349\n",
    "# # Index = [2,0,6,11,8,10]\n",
    "# Index = [2,0,6,9,8,10]\n",
    "## 第九组\n",
    "# train_bound = 888\n",
    "# val_bound = 1780\n",
    "# test_bound_1 = 2946\n",
    "# test_bound_2 = 3783\n",
    "# test_bound_3 = 4558\n",
    "# # Index = [2,6,0,8,7,15]\n",
    "# Index = [0,2,6,7,8,11]\n",
    "## 第十组\n",
    "train_bound = 532\n",
    "val_bound = 987\n",
    "test_bound_1 = 1533\n",
    "test_bound_2 = 2332\n",
    "test_bound_3 = 2898\n",
    "test_bound_4 = 3261\n",
    "test_bound_5 = 3690\n",
    "test_bound_6 = 4110\n",
    "Index = [1,2,7,8,10,11]"
   ],
   "id": "3aee24db50ef4671",
   "outputs": [],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:17:27.248557Z",
     "start_time": "2025-07-03T03:17:27.244623Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data.head()\n",
    "cols = list(data.columns[1:])\n",
    "cols = [cols[Index[0]], cols[Index[1]], cols[Index[2]], cols[Index[3]], cols[Index[4]], cols[Index[5]]]"
   ],
   "id": "c61ea55c856e945f",
   "outputs": [],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:18:31.731688Z",
     "start_time": "2025-07-03T03:18:31.727795Z"
    }
   },
   "cell_type": "code",
   "source": [
    "cols\n",
    "data['capacity'] = data['capacity'].ffill().bfill()\n",
    "depth = 5"
   ],
   "id": "2e76ac619c5fc499",
   "outputs": [],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:19:25.268304Z",
     "start_time": "2025-07-03T03:19:25.238998Z"
    }
   },
   "cell_type": "code",
   "source": [
    "My_data = data[:train_bound]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_train = X\n",
    "\n",
    "My_data = data[train_bound:val_bound]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_val = X\n",
    "\n",
    "My_data = data[val_bound:test_bound_1]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_test_1 = X\n",
    "\n",
    "My_data = data[test_bound_1:test_bound_2]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_test_2 = X\n",
    "\n",
    "My_data = data[test_bound_2:test_bound_3]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_test_3 = X\n",
    "\n",
    "\n",
    "## SCU数据集专有\n",
    "My_data = data[test_bound_3:test_bound_4]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_test_4 = X\n",
    "\n",
    "My_data = data[test_bound_4:test_bound_5]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_test_5 = X\n",
    "\n",
    "My_data = data[test_bound_5:test_bound_6]\n",
    "X = np.zeros((len(My_data), depth, len(cols)))\n",
    "for i, name in enumerate(cols):\n",
    "    for j in range(depth):\n",
    "        X[:, j, i] = My_data[name].shift(depth - j - 1).bfill()\n",
    "X_test_6 = X"
   ],
   "id": "b7147307d0ad6b36",
   "outputs": [],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:20:09.925770Z",
     "start_time": "2025-07-03T03:20:09.921529Z"
    }
   },
   "cell_type": "code",
   "source": "y = data['capacity'].shift(0).ffill().values",
   "id": "cb152694a064edec",
   "outputs": [],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:20:11.689343Z",
     "start_time": "2025-07-03T03:20:11.685768Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# X_train = X[:train_bound]\n",
    "# X_val = X[train_bound:val_bound]\n",
    "# X_test_1 = X[val_bound:test_bound_1]\n",
    "# X_test_2 = X[test_bound_1:test_bound_2]\n",
    "# X_test_3 = X[test_bound_2:test_bound_3]\n",
    "\n",
    "y_train  = y[:train_bound]\n",
    "y_val    = y[train_bound:val_bound]\n",
    "y_test_1 = y[val_bound:test_bound_1]\n",
    "y_test_2 = y[test_bound_1:test_bound_2]\n",
    "y_test_3 = y[test_bound_2:test_bound_3]\n",
    "\n",
    "## SCU数据集专有\n",
    "y_test_4 = y[test_bound_3:test_bound_4]\n",
    "y_test_5 = y[test_bound_4:test_bound_5]\n",
    "y_test_6 = y[test_bound_5:test_bound_6]"
   ],
   "id": "3142f9d03691e829",
   "outputs": [],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:20:13.834500Z",
     "start_time": "2025-07-03T03:20:13.831579Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 不需要重新归一化\n",
    "X_train_min = 0\n",
    "X_train_max = 1\n",
    "y_train_min = 0\n",
    "y_train_max = 1\n",
    "\n",
    "## 重新归一化\n",
    "# X_train_min, X_train_max = X_train.min(axis=0), X_train.max(axis=0)\n",
    "# y_train_min, y_train_max = y_train.min(axis=0), y_train.max(axis=0)"
   ],
   "id": "9c27ac941354912c",
   "outputs": [],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:20:16.182582Z",
     "start_time": "2025-07-03T03:20:16.175147Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X_train = (X_train - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "X_val = (X_val - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "X_test_1 = (X_test_1 - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "X_test_2 = (X_test_2 - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "X_test_3 = (X_test_3 - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "\n",
    "y_train = (y_train - y_train_min)/(y_train_max - y_train_min +1e-9)\n",
    "y_val = (y_val - y_train_min)/(y_train_max - y_train_min + 1e-9)\n",
    "y_test_1 = (y_test_1 - y_train_min)/(y_train_max - y_train_min + 1e-9)\n",
    "y_test_2 = (y_test_2 - y_train_min)/(y_train_max - y_train_min + 1e-9)\n",
    "y_test_3 = (y_test_3 - y_train_min)/(y_train_max - y_train_min + 1e-9)\n",
    "\n",
    "## SCU数据集专有\n",
    "X_test_4 = (X_test_4 - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "X_test_5 = (X_test_5 - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "X_test_6 = (X_test_6 - X_train_min)/(X_train_max - X_train_min + 1e-9)\n",
    "\n",
    "y_test_4 = (y_test_4 - y_train_min)/(y_train_max - y_train_min + 1e-9)\n",
    "y_test_5 = (y_test_5 - y_train_min)/(y_train_max - y_train_min + 1e-9)\n",
    "y_test_6 = (y_test_6 - y_train_min)/(y_train_max - y_train_min + 1e-9)"
   ],
   "id": "bb6eee5e7e6c8322",
   "outputs": [],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:20:18.262218Z",
     "start_time": "2025-07-03T03:20:18.245631Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X_train_t = torch.Tensor(X_train)\n",
    "X_val_t = torch.Tensor(X_val)\n",
    "X_test_t_1 = torch.Tensor(X_test_1)\n",
    "X_test_t_2 = torch.Tensor(X_test_2)\n",
    "X_test_t_3 = torch.Tensor(X_test_3)\n",
    "\n",
    "y_train_t = torch.Tensor(y_train)\n",
    "y_val_t = torch.Tensor(y_val)\n",
    "y_test_t_1 = torch.Tensor(y_test_1)\n",
    "y_test_t_2 = torch.Tensor(y_test_2)\n",
    "y_test_t_3 = torch.Tensor(y_test_3)\n",
    "\n",
    "## SCU数据集专有\n",
    "X_test_t_4 = torch.Tensor(X_test_4)\n",
    "X_test_t_5 = torch.Tensor(X_test_5)\n",
    "X_test_t_6 = torch.Tensor(X_test_6)\n",
    "\n",
    "y_test_t_4 = torch.Tensor(y_test_4)\n",
    "y_test_t_5 = torch.Tensor(y_test_5)\n",
    "y_test_t_6 = torch.Tensor(y_test_6)"
   ],
   "id": "1daddeee794d4bb0",
   "outputs": [],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:20:19.961623Z",
     "start_time": "2025-07-03T03:20:19.957212Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_loader  = DataLoader(TensorDataset(X_train_t, y_train_t), batch_size=64, shuffle=True)\n",
    "My_train_loader= DataLoader(TensorDataset(X_train_t, y_train_t), batch_size=64, shuffle=False)\n",
    "val_loader    = DataLoader(TensorDataset(X_val_t, y_val_t), batch_size=64, shuffle=False)\n",
    "test_loader_1 = DataLoader(TensorDataset(X_test_t_1, y_test_t_1), batch_size=64, shuffle=False)\n",
    "test_loader_2 = DataLoader(TensorDataset(X_test_t_2, y_test_t_2), batch_size=64, shuffle=False)\n",
    "test_loader_3 = DataLoader(TensorDataset(X_test_t_3, y_test_t_3), batch_size=64, shuffle=False)\n",
    "\n",
    "## SCU数据集专有\n",
    "test_loader_4 = DataLoader(TensorDataset(X_test_t_4, y_test_t_4), batch_size=64, shuffle=False)\n",
    "test_loader_5 = DataLoader(TensorDataset(X_test_t_5, y_test_t_5), batch_size=64, shuffle=False)\n",
    "test_loader_6 = DataLoader(TensorDataset(X_test_t_6, y_test_t_6), batch_size=64, shuffle=False)"
   ],
   "id": "787e16e6b62499ed",
   "outputs": [],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:21:21.351630Z",
     "start_time": "2025-07-03T03:21:19.666694Z"
    }
   },
   "cell_type": "code",
   "source": [
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "model = IMVTensorLSTM(X_train_t.shape[2], 1, 512).to(device)\n",
    "\n",
    "opt = torch.optim.Adam(model.parameters(), lr=0.001)\n",
    "epoch_scheduler = torch.optim.lr_scheduler.StepLR(opt, 20, gamma=0.9)"
   ],
   "id": "2f3d134174e53650",
   "outputs": [],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:21:26.739389Z",
     "start_time": "2025-07-03T03:21:26.647145Z"
    }
   },
   "cell_type": "code",
   "source": [
    "epochs = 100\n",
    "loss = nn.MSELoss()\n",
    "patience = 35\n",
    "min_val_loss = 9999\n",
    "counter = 0\n",
    "for i in range(epochs):\n",
    "    mse_train = 0\n",
    "    for batch_x, batch_y in train_loader:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        opt.zero_grad()\n",
    "        y_pred, alphas, betas = model(batch_x)\n",
    "        y_pred = y_pred.squeeze(1)\n",
    "        l = loss(y_pred, batch_y)\n",
    "        l.backward()\n",
    "        mse_train += l.item()*batch_x.shape[0]\n",
    "        opt.step()\n",
    "    epoch_scheduler.step()\n",
    "    with torch.no_grad():\n",
    "        mse_val = 0\n",
    "        preds = []\n",
    "        true = []\n",
    "        for batch_x, batch_y in My_train_loader:\n",
    "            batch_x = batch_x.cuda()\n",
    "            batch_y = batch_y.cuda()\n",
    "            output, alphas, betas = model(batch_x)\n",
    "            output = output.squeeze(1)\n",
    "            preds.append(output.detach().cpu().numpy())\n",
    "            true.append(batch_y.detach().cpu().numpy())\n",
    "            mse_val += loss(output, batch_y).item()*batch_x.shape[0]\n",
    "    preds = np.concatenate(preds)\n",
    "    true = np.concatenate(true)\n",
    "    \n",
    "    if min_val_loss > mse_val**0.5:\n",
    "        min_val_loss = mse_val**0.5\n",
    "        print(\"Saving...\")\n",
    "        torch.save(model.state_dict(), \"imv_tensor_lstm_soh.pt\")\n",
    "        counter = 0\n",
    "    else: \n",
    "        counter += 1\n",
    "    \n",
    "    # if counter == patience:\n",
    "    #     break\n",
    "    print(\"Iter: \", i, \"train: \", (mse_train/len(X_train_t))**0.5, \"val: \", (mse_val/len(X_val_t))**0.5)"
   ],
   "id": "9c8e167469d32de8",
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "Torch not compiled with CUDA enabled",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAssertionError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[52]\u001B[39m\u001B[32m, line 9\u001B[39m\n\u001B[32m      7\u001B[39m mse_train = \u001B[32m0\u001B[39m\n\u001B[32m      8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m train_loader:\n\u001B[32m----> \u001B[39m\u001B[32m9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n\u001B[32m     11\u001B[39m     opt.zero_grad()\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\torch\\cuda\\__init__.py:363\u001B[39m, in \u001B[36m_lazy_init\u001B[39m\u001B[34m()\u001B[39m\n\u001B[32m    358\u001B[39m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mRuntimeError\u001B[39;00m(\n\u001B[32m    359\u001B[39m         \u001B[33m\"\u001B[39m\u001B[33mCannot re-initialize CUDA in forked subprocess. To use CUDA with \u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m    360\u001B[39m         \u001B[33m\"\u001B[39m\u001B[33mmultiprocessing, you must use the \u001B[39m\u001B[33m'\u001B[39m\u001B[33mspawn\u001B[39m\u001B[33m'\u001B[39m\u001B[33m start method\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m    361\u001B[39m     )\n\u001B[32m    362\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28mhasattr\u001B[39m(torch._C, \u001B[33m\"\u001B[39m\u001B[33m_cuda_getDeviceCount\u001B[39m\u001B[33m\"\u001B[39m):\n\u001B[32m--> \u001B[39m\u001B[32m363\u001B[39m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAssertionError\u001B[39;00m(\u001B[33m\"\u001B[39m\u001B[33mTorch not compiled with CUDA enabled\u001B[39m\u001B[33m\"\u001B[39m)\n\u001B[32m    364\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m _cudart \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[32m    365\u001B[39m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAssertionError\u001B[39;00m(\n\u001B[32m    366\u001B[39m         \u001B[33m\"\u001B[39m\u001B[33mlibcudart functions unavailable. It looks like you have a broken build?\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m    367\u001B[39m     )\n",
      "\u001B[31mAssertionError\u001B[39m: Torch not compiled with CUDA enabled"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "model.load_state_dict(torch.load(\"imv_tensor_lstm_soh.pt\"))",
   "id": "f50420e7e9650984",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:37.816005Z",
     "start_time": "2025-07-03T03:16:37.773046Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 训练样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in My_train_loader:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item()*batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "6c9c4fb01f7aaad",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'My_train_loader' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[12]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m My_train_loader:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'My_train_loader' is not defined"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds*(y_train_max - y_train_min) + y_train_min)*0.2+0.8\n",
    "true = ((true*(y_train_max - y_train_min) + y_train_min))*0.2+0.8\n",
    "mae = mean_absolute_error(true, preds)*100\n",
    "mape = np.mean(np.abs((true - preds)/true))*100\n",
    "rmse = sqrt(mean_squared_error(true, preds))*100\n",
    "R2 = r2_score(true, preds)*100\n",
    "mae, mape, rmse, R2"
   ],
   "id": "8b42ced79f65fcd4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:37.914819Z",
     "start_time": "2025-07-03T03:16:37.849927Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "1a784dc23c417a91",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[13]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "mae = np.zeros(7)\n",
    "mape = np.zeros(7)\n",
    "rmse = np.zeros(7)\n",
    "R2 = np.zeros(7)"
   ],
   "id": "e185a063fa247ff9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:37.988663Z",
     "start_time": "2025-07-03T03:16:37.945202Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 验证样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in val_loader:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item()*batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "dae9e768a16f982e",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'val_loader' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[14]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m val_loader:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'val_loader' is not defined"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[0] = mean_absolute_error(true, preds) * 100\n",
    "mape[0] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[0] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[0] = r2_score(true, preds) * 100\n",
    "mae[0], mape[0], rmse[0], R2[0]"
   ],
   "id": "4568a4dee7e7f833",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.014027Z",
     "start_time": "2025-07-03T03:16:38.010134Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.savetxt(\"preds_01_01.txt\",preds)\n",
    "# np.savetxt(\"true_01_01.txt\",true)"
   ],
   "id": "c676f5b024fd3c55",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.102054Z",
     "start_time": "2025-07-03T03:16:38.065969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "d32c62ce25c6577c",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[16]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.165487Z",
     "start_time": "2025-07-03T03:16:38.123204Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 第一个测试样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in test_loader_1:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item() * batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "17c3966c5aaccb6a",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_loader_1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[17]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m test_loader_1:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'test_loader_1' is not defined"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[1] = mean_absolute_error(true, preds) * 100\n",
    "mape[1] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[1] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[1] = r2_score(true, preds) * 100\n",
    "mae[1], mape[1], rmse[1], R2[1]"
   ],
   "id": "b30fbfc69864e651",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.188668Z",
     "start_time": "2025-07-03T03:16:38.186840Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.savetxt(\"preds_01_02.txt\",preds)\n",
    "# np.savetxt(\"true_01_02.txt\",true)"
   ],
   "id": "2b65eae65b6e2e86",
   "outputs": [],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.241374Z",
     "start_time": "2025-07-03T03:16:38.206905Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "972632cbcbcb509a",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[19]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.304616Z",
     "start_time": "2025-07-03T03:16:38.264989Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 第二个测试样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in test_loader_2:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item() * batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "2aebc6ed01c38797",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_loader_2' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[20]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m test_loader_2:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'test_loader_2' is not defined"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[2] = mean_absolute_error(true, preds) * 100\n",
    "mape[2] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[2] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[2] = r2_score(true, preds) * 100\n",
    "mae[2], mape[2], rmse[2], R2[2]"
   ],
   "id": "c67150d5095aba0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.341889Z",
     "start_time": "2025-07-03T03:16:38.338970Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.savetxt(\"preds_01_03.txt\",preds)\n",
    "# np.savetxt(\"true_01_03.txt\",true)"
   ],
   "id": "27d98f0dd2d1944d",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.405520Z",
     "start_time": "2025-07-03T03:16:38.370426Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "f4383238cf8e9b44",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[22]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.456402Z",
     "start_time": "2025-07-03T03:16:38.410890Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 第三个测试样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in test_loader_3:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item() * batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "8114bc5eb8319ecc",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_loader_3' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[23]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m test_loader_3:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'test_loader_3' is not defined"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[3] = mean_absolute_error(true, preds) * 100\n",
    "mape[3] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[3] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[3] = r2_score(true, preds) * 100\n",
    "mae[3], mape[3], rmse[3], R2[3]"
   ],
   "id": "5f4054aa5639f830",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.493066Z",
     "start_time": "2025-07-03T03:16:38.490145Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.savetxt(\"preds_01_04.txt\",preds)\n",
    "# np.savetxt(\"true_01_04.txt\",true)"
   ],
   "id": "e733a62fe6bfce34",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.562908Z",
     "start_time": "2025-07-03T03:16:38.531027Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "a147d5476a8bc5a2",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[25]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.631429Z",
     "start_time": "2025-07-03T03:16:38.591897Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 第四个测试样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in test_loader_4:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item() * batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "96332155675d25b9",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_loader_4' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[26]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m test_loader_4:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'test_loader_4' is not defined"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[4] = mean_absolute_error(true, preds) * 100\n",
    "mape[4] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[4] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[4] = r2_score(true, preds) * 100\n",
    "mae[4], mape[4], rmse[4], R2[4]"
   ],
   "id": "951b97db4c31fe49",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.691318Z",
     "start_time": "2025-07-03T03:16:38.656886Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "2ce5a4c081feca2",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[27]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.692260Z",
     "start_time": "2025-07-03T03:16:38.692260Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 第五个测试样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in test_loader_5:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item() * batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "77c16533f4adc3d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.758842Z",
     "start_time": "2025-07-03T03:16:38.727082Z"
    }
   },
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[5] = mean_absolute_error(true, preds) * 100\n",
    "mape[5] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[5] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[5] = r2_score(true, preds) * 100\n",
    "mae[5], mape[5], rmse[5], R2[5]"
   ],
   "id": "7b42f643b27009f9",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'y_train_max' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[28]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m preds = (preds * (y_train_max - y_train_min) + y_train_min) * \u001B[32m0.2\u001B[39m + \u001B[32m0.8\u001B[39m\n\u001B[32m      2\u001B[39m true = ((true * (y_train_max - y_train_min) + y_train_min)) * \u001B[32m0.2\u001B[39m + \u001B[32m0.8\u001B[39m\n\u001B[32m      3\u001B[39m mae[\u001B[32m5\u001B[39m] = mean_absolute_error(true, preds) * \u001B[32m100\u001B[39m\n",
      "\u001B[31mNameError\u001B[39m: name 'y_train_max' is not defined"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "6900718bcac809ab",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.829528Z",
     "start_time": "2025-07-03T03:16:38.790811Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 第六个测试样本\n",
    "with torch.no_grad():\n",
    "    mse_val = 0\n",
    "    preds = []\n",
    "    true = []\n",
    "    alphas = []\n",
    "    betas = []\n",
    "    for batch_x, batch_y in test_loader_5:\n",
    "        batch_x = batch_x.cuda()\n",
    "        batch_y = batch_y.cuda()\n",
    "        output, a, b = model(batch_x)\n",
    "        output = output.squeeze(1)\n",
    "        preds.append(output.detach().cpu().numpy())\n",
    "        true.append(batch_y.detach().cpu().numpy())\n",
    "        alphas.append(a.detach().cpu().numpy())\n",
    "        betas.append(b.detach().cpu().numpy())\n",
    "        mse_val += loss(output, batch_y).item() * batch_x.shape[0]\n",
    "preds = np.concatenate(preds)\n",
    "true = np.concatenate(true)"
   ],
   "id": "c431ce3d8a0f864f",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_loader_5' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[29]\u001B[39m\u001B[32m, line 8\u001B[39m\n\u001B[32m      6\u001B[39m alphas = []\n\u001B[32m      7\u001B[39m betas = []\n\u001B[32m----> \u001B[39m\u001B[32m8\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m batch_x, batch_y \u001B[38;5;129;01min\u001B[39;00m test_loader_5:\n\u001B[32m      9\u001B[39m     batch_x = batch_x.cuda()\n\u001B[32m     10\u001B[39m     batch_y = batch_y.cuda()\n",
      "\u001B[31mNameError\u001B[39m: name 'test_loader_5' is not defined"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "preds = (preds * (y_train_max - y_train_min) + y_train_min) * 0.2 + 0.8\n",
    "true = ((true * (y_train_max - y_train_min) + y_train_min)) * 0.2 + 0.8\n",
    "mae[6] = mean_absolute_error(true, preds) * 100\n",
    "mape[6] = np.mean(np.abs((true - preds) / true)) * 100\n",
    "rmse[6] = sqrt(mean_squared_error(true, preds)) * 100\n",
    "R2[6] = r2_score(true, preds) * 100\n",
    "mae[6], mape[6], rmse[6], R2[6]"
   ],
   "id": "7dbf110cac3925f4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.895274Z",
     "start_time": "2025-07-03T03:16:38.862943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(preds)\n",
    "plt.plot(true)\n",
    "plt.show()"
   ],
   "id": "302c387ef93c2d14",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[30]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m10\u001B[39m, \u001B[32m5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.plot(preds)\n\u001B[32m      3\u001B[39m plt.plot(true)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "Mean_mae = np.mean(mae)\n",
    "Mean_mape = np.mean(mape)\n",
    "Mean_rmse = np.mean(rmse)\n",
    "Mean_R2 = np.mean(R2)\n",
    "Mean_mae, Mean_mape, Mean_rmse, Mean_R2"
   ],
   "id": "cc5cd13085bde0c3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.955282Z",
     "start_time": "2025-07-03T03:16:38.930309Z"
    }
   },
   "cell_type": "code",
   "source": [
    "alphas = np.concatenate(alphas)\n",
    "betas = np.concatenate(betas)"
   ],
   "id": "7807a8bed10e839c",
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "need at least one array to concatenate",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mValueError\u001B[39m                                Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[31]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m alphas = np.concatenate(alphas)\n\u001B[32m      2\u001B[39m betas = np.concatenate(betas)\n",
      "\u001B[31mValueError\u001B[39m: need at least one array to concatenate"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:38.986130Z",
     "start_time": "2025-07-03T03:16:38.961105Z"
    }
   },
   "cell_type": "code",
   "source": [
    "alphas = alphas.mean(axis=0)\n",
    "betas = betas.mean(axis=0)"
   ],
   "id": "b8687ca983124132",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'mean'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[32]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m alphas = alphas.mean(axis=\u001B[32m0\u001B[39m)\n\u001B[32m      2\u001B[39m betas = betas.mean(axis=\u001B[32m0\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: 'list' object has no attribute 'mean'"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:39.046248Z",
     "start_time": "2025-07-03T03:16:39.014988Z"
    }
   },
   "cell_type": "code",
   "source": [
    "alphas = alphas[..., 0]\n",
    "betas = betas[..., 0]"
   ],
   "id": "2cfc4115c7132540",
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not tuple",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mTypeError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[33]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m alphas = alphas[..., \u001B[32m0\u001B[39m]\n\u001B[32m      2\u001B[39m betas = betas[..., \u001B[32m0\u001B[39m]\n",
      "\u001B[31mTypeError\u001B[39m: list indices must be integers or slices, not tuple"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:39.048700Z",
     "start_time": "2025-07-03T03:16:39.047767Z"
    }
   },
   "cell_type": "code",
   "source": "alphas = alphas.transpose(1, 0)",
   "id": "4a13aa0b3caf3bac",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:39.109740Z",
     "start_time": "2025-07-03T03:16:39.106819Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# fig, ax = plt.subplots(figsize=(10, 10))\n",
    "# im = ax.imshow(alphas)\n",
    "# ax.set_xticks(np.arange(X_train_t.shape[1]))\n",
    "# ax.set_yticks(np.arange(len(cols)))\n",
    "# ax.set_xticklabels([\"t-\"+str(i) for i in np.arange(X_train_t.shape[1], -1, -1)])\n",
    "# ax.set_yticklabels(list(cols))\n",
    "# for i in range(len(cols)):\n",
    "#     for j in range(X_train_t.shape[1]):\n",
    "#         text = ax.text(j, i, round(alphas[i, j], 3),\n",
    "#                        ha=\"center\", va=\"center\", color=\"w\")\n",
    "# ax.set_title(\"Importance of features and timesteps\")\n",
    "# #fig.tight_layout()\n",
    "# plt.show()"
   ],
   "id": "b564c9baaa6916a4",
   "outputs": [],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:39.202913Z",
     "start_time": "2025-07-03T03:16:39.170975Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(5, 2.5))\n",
    "plt.title(\"Feature importance\")\n",
    "plt.bar(range(len(cols)), betas)\n",
    "plt.xticks(ticks=range(len(cols)), labels=cols, rotation=90)"
   ],
   "id": "7f448093fe67193d",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[35]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m plt.figure(figsize=(\u001B[32m5\u001B[39m, \u001B[32m2.5\u001B[39m))\n\u001B[32m      2\u001B[39m plt.title(\u001B[33m\"\u001B[39m\u001B[33mFeature importance\u001B[39m\u001B[33m\"\u001B[39m)\n\u001B[32m      3\u001B[39m plt.bar(\u001B[38;5;28mrange\u001B[39m(\u001B[38;5;28mlen\u001B[39m(cols)), betas)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "print(betas)",
   "id": "db7c74d4387eb16c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "Sorted_betas = sorted(betas, reverse=True)\n",
    "print(Sorted_betas[:6])"
   ],
   "id": "d53516bc05b96d90",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T03:16:39.263593Z",
     "start_time": "2025-07-03T03:16:39.227823Z"
    }
   },
   "cell_type": "code",
   "source": [
    "betaCurrent = sum(betas[0:6])\n",
    "betaVoltage = sum(betas[6:12])\n",
    "betaTemperature = sum(betas[12:18])\n",
    "betaCategory = np.array([betaCurrent, betaVoltage, betaTemperature])\n",
    "plt.figure(figsize=(5, 2.5))\n",
    "plt.title(\"Category Feature importance\")\n",
    "plt.bar(range(len(betaCategory)), betaCategory)\n",
    "plt.xticks(ticks=range(len(betaCategory)), rotation=90)"
   ],
   "id": "cab28dcf8e82bb3a",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'matplotlib' has no attribute 'figure'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[36]\u001B[39m\u001B[32m, line 5\u001B[39m\n\u001B[32m      3\u001B[39m betaTemperature = \u001B[38;5;28msum\u001B[39m(betas[\u001B[32m12\u001B[39m:\u001B[32m18\u001B[39m])\n\u001B[32m      4\u001B[39m betaCategory = np.array([betaCurrent, betaVoltage, betaTemperature])\n\u001B[32m----> \u001B[39m\u001B[32m5\u001B[39m plt.figure(figsize=(\u001B[32m5\u001B[39m, \u001B[32m2.5\u001B[39m))\n\u001B[32m      6\u001B[39m plt.title(\u001B[33m\"\u001B[39m\u001B[33mCategory Feature importance\u001B[39m\u001B[33m\"\u001B[39m)\n\u001B[32m      7\u001B[39m plt.bar(\u001B[38;5;28mrange\u001B[39m(\u001B[38;5;28mlen\u001B[39m(betaCategory)), betaCategory)\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\anaconda\\envs\\general\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:218\u001B[39m, in \u001B[36mcaching_module_getattr.<locals>.__getattr__\u001B[39m\u001B[34m(name)\u001B[39m\n\u001B[32m    216\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m name \u001B[38;5;129;01min\u001B[39;00m props:\n\u001B[32m    217\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m props[name].\u001B[34m__get__\u001B[39m(instance)\n\u001B[32m--> \u001B[39m\u001B[32m218\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\n\u001B[32m    219\u001B[39m     \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mmodule \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mcls\u001B[39m.\u001B[34m__module__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mname\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: module 'matplotlib' has no attribute 'figure'"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "betaCharge_1A = betas[0] + betas[6] + betas[12]\n",
    "betaCharge_3V6 = betas[1] + betas[7] + betas[13]\n",
    "betaCharge_20SOC = betas[2] + betas[8] + betas[14]\n",
    "betaDischarge_20SOC = betas[3] + betas[9] + betas[15]\n",
    "betaDischarge_80SOC = betas[4] + betas[10] + betas[16]\n",
    "betaDischarge_20to80SOC = betas[5] + betas[11] + betas[17]\n",
    "betaStage = np.array([betaCharge_1A, betaCharge_3V6, betaCharge_20SOC, betaDischarge_20SOC, betaDischarge_80SOC,\n",
    "                      betaDischarge_20to80SOC])\n",
    "plt.figure(figsize=(5, 2.5))\n",
    "plt.title(\"Stage Feature importance\")\n",
    "plt.bar(range(len(betaStage)), betaStage)\n",
    "plt.xticks(ticks=range(len(betaStage)), rotation=90)"
   ],
   "id": "3b0dd853bddf1754",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
